#include "PhotonLineShape.hh"
#include "TString.h"

Double_t LineShapeCDEX(Double_t* x, Double_t* par)
{
    if((x[0] - par[1]) < -500 )
    {
        return 0.;
    }
    Double_t expTerm = TMath::Exp(TMath::Power(par[3], 2) / (2 * TMath::Power(par[2], 2)) - (x[0] - par[1]) / par[2]);
    Double_t erfTerm = 1 + TMath::Erf((par[2] * (x[0] - par[1]) - TMath::Power(par[3], 2)) / (TMath::Sqrt(2) * par[2] * par[3]));
    return (par[0] / (2 * par[2]) * expTerm * erfTerm);
}

PhotonLineShape::PhotonLineShape(int iPMT, int iPhoton, double Amp, double Time, double Tau, double Smear, double StartTime, double StopTime):
    m_Amp(Amp), m_Time(Time), m_Tau(Tau), m_Smear(Smear)
{
    m_LineShape = new TF1(TString::Format("LineShape%d_%d", iPMT, iPhoton), LineShapeCDEX, StartTime, StopTime, 4);
    m_LineShape->SetParameters(m_Amp, m_Time, m_Tau, m_Smear);
}

PhotonLineShape::~PhotonLineShape()
{
    delete m_LineShape;
}

double PhotonLineShape::Evaluate(double time)
{
    return m_LineShape->Eval(time);
}